package com.test;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class MailSend {
	public static void main(String[] args) {
		sendMails();
		for (int i = 0; i < 500; i++) {
		}
	}
	
	public static void sendMails() {
		try {
			String userName = "najl@chinaskycloud.com";
		    String userPswd = "njl19835%";
		    String smtpServer = "MAIL.CLOUD-VALLEY.COM";
		    String fromEmail = userName;
		    String toEmail = "dalinjava@163.com";
		
		    
		    
		    
		    
		    
		    
		    
		    
		    
		    
		    
		    
		    
		    
		    
		    
		    Properties mailProps = new Properties();
		    mailProps.put("mail.smtp.host", smtpServer);
		    mailProps.put("mail.smtp.auth", "true");
		    PopupAuthenticator auth = new PopupAuthenticator(userName,userPswd);
		    Session session = Session.getDefaultInstance(mailProps, auth);
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(fromEmail));
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
			message.setSubject("刘司好");
			message.setText("你让我发的");
			message.saveChanges();
			Transport.send(message);
			System.out.println("111111111");
		} catch (Exception ex) {
			System.err.println("邮件发送失败的原因是：" + ex.getMessage());
			System.err.println("具体错误原因：");
			ex.printStackTrace(System.err);
		}
	}
}

class PopupAuthenticator extends Authenticator {
	private String username;
	private String password;
	public PopupAuthenticator(String username,String password){
		this.username=username;
		this.password=password;
	}
	public PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(this.username, this.password);
	}
}
